<?php
namespace Iden\Controller;
use IdenApi\Api\IndexApi;

class IdentifyController extends \Think\Controller {
    
    public function doGet() {

        $httpdata = $_POST['paramJson'];
       	$httpdata = json_decode($httpdata,true);

        $rcdata['user_id'] = $httpdata['user_id'];
        $rcdata['cardno'] = $httpdata['cardno'];
        $rcdata['name'] = $httpdata['name'];
        $rcdata['face_card'] = $httpdata['face_card'];
        $rcdata['back_card'] = $httpdata['back_card'];

        //假数据
        // $rcdata['user_id'] = 3;
        // $rcdata['cardno'] = 1233777777;
        // $rcdata['name'] = 1;
        // $rcdata['face_card'] = 1;
        // $rcdata['back_card'] = 1;

        //本地查询有没有已经实名认证的数据，没有再调用第三方
        $IdentifyLog = D('IdentifyLog');
        $res = $IdentifyLog->selectIden(array('cardno'=>$rcdata['cardno'],'name'=>$rcdata['name']));

        //存在认证过的数据
        if($res['state'] == 1){
            //查询实名数据是否是此用户的，不是则增加/改变用户实名日志
            $IdentifyLog = D('IdentifyLog');
            //是此用户的数据
            if($res['user_id'] == $rcdata['user_id']){
              successOutputJson(['info' => '身份认证成功']);
            }else{
              //判断用户存不存在，存在则修改
              $data['cardno'] = $res['cardno'];
              $data['name'] = $res['name'];
              $data['face_card'] = $res['face_card'];
              $data['back_card'] = $res['back_card'];
              $data['sex'] = $res['sex'];
              $data['address'] = $res['address'];
              $data['birthday'] = $res['birthday'];
              $data['state'] = $res['state'];

              $user_data = $IdentifyLog->selectIden(array('user_id'=>$rcdata['user_id']));

              //用户存在，则修改
              if($user_data){
                $data['up_time'] = time();
                // successOutputJson(['info' => $data['up_time']]);
                $IdentifyLog->saveIden(array('user_id'=>$rcdata['user_id']),$data);
              }else{
                $data['user_id'] = $rcdata['user_id'];
                $data['add_time'] = $data['up_time'] = time();
                $IdentifyLog->addIden($data);
              }
            }
            successOutputJson(['info' => '身份认证成功']);
        }else{
          //本地没有数据，调用第三方
            $Iden = new IndexApi;
            $Iden->doGet($rcdata['cardno'],$rcdata['name']);
        }

        //返回假数据
        // $data = array(
     //   		'data'=>array(
     //   				'sex'=>'女',
     //   				'address'=>'山东省-潍坊市-寒亭区',
     //   				'birthday'=>'1981-11-30',
     //   			),
     //   		'resp'=>array(
					// 	'code'=>14,
					// 	'desc'=>'不匹配'

					// ),
     //   	 );
       	// $return = json_encode($data);
       	 // var_dump($return);

       	 //处理结果，成功则存数据库           	
       	if($Iden){
       		$data = json_decode($return,true);

       		// var_dump($data['resp']['code']);
       		if($data['resp']['code'] == 0){
       			if($data['data']['sex'] == '男'){
       				$rcdata['sex'] = 0;
       			}elseif($data['data']['sex'] == '女'){
       				$rcdata['sex'] = 1;
       			}
	        	$rcdata['address'] = $data['data']['address'];
	        	$rcdata['birthday'] = $data['data']['birthday'];
	        	$rcdata['add_time'] = $rcdata['up_time']= time();
       			//存日志
        		$rcdata['state'] = 1;
        		$IdentifyLog = D('IdentifyLog');
        		//判断用户存不存在，存在则修改
        		$res = $IdentifyLog->selectIden(array('user_id'=>$rcdata['user_id']));
        		if($res){
        			$IdentifyLog->saveIden(array('user_id'=>$rcdata['user_id']),$rcdata);

        		}else{
        			$IdentifyLog->addIden($rcdata);
        		}

            	successOutputJson(['info' => '身份认证成功']);

       		}elseif($data['resp']['code'] == 5){
       			if($data['data']['sex'] == '男'){
       				$rcdata['sex'] = 0;
       			}elseif($data['data']['sex'] == '女'){
       				$rcdata['sex'] = 1;
       			}
	        	$rcdata['address'] = $data['data']['address'];
	        	$rcdata['birthday'] = $data['data']['birthday'];
	        	$rcdata['add_time'] = $rcdata['up_time']= time();
       			//存日志
        		$rcdata['state'] = 2;
        		$IdentifyLog = D('IdentifyLog');
        		$res = $IdentifyLog->selectIden(array('user_id'=>$rcdata['user_id']));
        		if($res){
        			$IdentifyLog->saveIden(array('user_id'=>$rcdata['user_id']),$rcdata);
        		}else{
        			$IdentifyLog->addIden($rcdata);
        		}

        		errorOutputJson('不匹配');

       		}elseif($data['resp']['code'] == 14){
        		errorOutputJson('无此身份证号码');
       		}elseif($data['resp']['code'] == 96){
        		errorOutputJson('交易失败，请稍后重试');
       		}
       	}else{
        	 errorOutputJson('交易失败，请稍后重试');
       	}


	}
}